<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function  uuid()
{
    $chars = md5(uniqid(mt_rand(), true));
    $uuid = substr ( $chars, 0, 8 )
        . substr ( $chars, 8, 4 )
        . substr ( $chars, 12, 4 )
        . substr ( $chars, 16, 4 )
        . substr ( $chars, 20, 12 );
    return $uuid ;
}

/**
 * @return Token
 * 时间戳 md5
 *
 */

function setToken(){
    $time = time();
    $token = md5($time);
    return $token;
}

/**
 * session有效判断
 */
function hasSession($id){
    $time = Session::get('admin_'.$id);
    $expire =\think\Config::get('session')['expire'];
    if($time['time'] + $expire >= time()){
        return true;
    }
    return false;
}

/**
 * @return token 认证
 */
function hasToken($id,$user_token){
    $token = \think\Session::get('admin_'.$id);
//    $token = Session::get('admin_'.$id);
//    return $token;
    if($token['token'] === $user_token){
        return true;
    }
    return false;
}

/**
 *多维数组变为一维数组
 */
function reduceArray($array) {
    $return = [];
    array_walk_recursive($array, function ($x) use (&$return) {
        $return[] = $x;
    });
    return $return;
}

/**
 * 检查手机号码格式
 * @param $mobile 手机号码
 */
function check_mobile($mobile){
    if(preg_match('/1[34578]\d{9}$/',$mobile)) {
        return true;
    }
    return false;
}

//PHP stdClass Object转array
function object_array($array) {
    if(is_object($array)) {
        $array = (array)$array;
    } if(is_array($array)) {
        foreach($array as $key=>$value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}

/**
 * 网页数据抓取
 */
function curl_get_https($url){
    // echo $url;

    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL,$url); //指定url
    curl_setopt($curl, CURLOPT_HEADER, 0); //忽略header头信息
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设定请求后返回结果
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在

    $tmpInfo = curl_exec($curl);     //返回api的json对象
    // var_dump($tmpInfo);
    // exit();
    if (curl_errno($curl)) {
        echo 'Errno '.curl_error($curl);//捕抓异常
    }
    //关闭URL请求
    curl_close($curl);
    return $tmpInfo;    //返回json对象
}

/**
 * 图片上传腾讯云云存储
 */
function upload_img( $cosClient , $file ,$url){
    $type = $file->getInfo('type');
    $index = '/';
    $_toIndex = stripos($type,$index);
    $lasttype = substr($type,$_toIndex+1,strlen($type));
    try {
        $result = $cosClient->putObject(
            [
                'Bucket' => "milk-1256444651",
                'Key' =>$url.'/'.date("Y-m-d") . "/" . md5(microtime()) . '.'.$lasttype,
                'Body' => fopen($file->getInfo('tmp_name'), 'rb'),
//                "ACL" => "public-read-write",
//                "ContentType" => "image/jpeg"
                "ContentType" => $type

            ]
        );
        return json(
            [
                "code" => 0,
                "msg" => '上传成功',
                "data" => [
                    "src" =>  str_replace("http://milk-1256444651.cos.ap-chengdu.myqcloud.com", "https://milk.zjinyi.cn", $result['ObjectURL']),
//                    "title" => "图标"
                ]
            ]
        );
    } catch (\Exception $e) {
        return json(
            [
                "code" => 1,
                "msg" => $e,
                "data" => [
                    "src" => "",
                    "title" => ""
                ]
            ]
        );
    }
}

